package com.example.administrator.managesystem.presenter.login;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.managesystem.bean.CodeBean;
import com.example.administrator.managesystem.bean.ForgetPsdBean;
import com.example.administrator.managesystem.bean.LoginBean;
import com.example.administrator.managesystem.bean.RegisterBean;
import com.example.administrator.managesystem.ui.LoginView;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by TR-AND2 on 2017/12/8.
 */


public class LoginPresenter {
    private LoginView loginView;
    public LoginPresenter(LoginView loginView) {
        this.loginView = loginView;
    }

    public void CodeRequest(String telephone,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).code(telephone)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<CodeBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        Log.e("msg","t.getMessage():"+t.getMessage());
                    }

                    @Override
                    public void onNext(CodeBean codeBean) {
                     if(codeBean.getStatus()==200){
                            loginView.startCountDown();
                           ToastUtils.showToast(context,codeBean.getData());
                    }else{
                         ToastUtils.showToast(context,codeBean.getMsg());
                     }

                    }

                });
    }
    public void OneLoginRequest(String telephone,String verify_code,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).oneShotLogin(telephone,verify_code)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                       if(loginBean.getStatus()==200){
                           ShareUtil.saveUserData(context,loginBean.getData().getId(),loginBean.getData().getName(),loginBean.getData().getHead_img(),loginBean.getData().getToken());
                           loginView.navigateToHome();
                       }else {
                           ToastUtils.showToast(context,loginBean.getMsg());
                           loginView.showMessage(null);
                       }

                    }

                });
    }
    public void loginRequest(String telephone,String password,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).login(telephone,password)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                        loginView.showMessage(null);
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if(loginBean.getStatus()==200){
                            ShareUtil.saveUserData(context,loginBean.getData().getId(),loginBean.getData().getName(),loginBean.getData().getHead_img(),loginBean.getData().getToken());
                            loginView.navigateToHome();
                        }else {
                            ToastUtils.showToast(context,loginBean.getMsg());
                            loginView.showMessage(null);
                        }

                    }

                });
    }
    public void ForgetPsdRequest(String telephone,String verify_code,String psd_new,String psd_new_confirm,final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).forgetPsd(telephone,verify_code,psd_new,psd_new_confirm)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<ForgetPsdBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(ForgetPsdBean forgetPsdBean) {
                       if(forgetPsdBean.getStatus()==200){
                           ShareUtil.saveUserData(context,forgetPsdBean.getData().getId(),forgetPsdBean.getData().getName(),forgetPsdBean.getData().getHead_img(),forgetPsdBean.getData().getToken());
                           loginView.navigateToHome();
                       }else{
                           ToastUtils.showToast(context,forgetPsdBean.getMsg());
                           loginView.showMessage(null);
                       }

                    }

                });
    }

    public void registerRequest(String type,String name,String img_url,String phone,String verify_code,String province,String city,String area,String addr,final Context context){
        if(img_url==null){ return; }
        File file = new File(img_url);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("id_card", file.getName(), requestFile);

        ServiceManager.getInstance(context).getService(GuideAPI.class).register(phone,type,name,verify_code,province,city,area,addr,body)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<RegisterBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(RegisterBean registerBean) {
                      if(registerBean.getStatus()==200){
                          ToastUtils.showToast(context,registerBean.getData());
                          loginView.navigateToHome();
                      }else{
                          ToastUtils.showToast(context,registerBean.getMsg());
                          loginView.showMessage(null);
                      }
                    }

                });
    }


}
